package com.nbicc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbicc.pojo.Finan;
import com.nbicc.service.FinanService;
import com.nbicc.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/5/10 20:36
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/finan")
public class FinanController {
    @Autowired
    private FinanService finanService;

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/selectAll")
    public Map<Object,Object> selectAll(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                        @RequestParam(value = "limit",defaultValue = "5")Integer pageSize){
        System.out.println("--selectAll--");
        HashMap<Object, Object> map = new HashMap<>();
        PageHelper.startPage(page,pageSize);
        PageInfo<Finan> pageInfo = finanService.selectAll();
        Double totalRow = finanService.totalRow();
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("grantsum",totalRow);
        JsonData jsonData = new JsonData();
        map.put("code",0);
        map.put("msg","");
        map.put("count",(int)pageInfo.getTotal());
        map.put("totalRow",map1);
        map.put("data",pageInfo.getList());
        return map;
    }
    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    public JsonData deleteById(@PathVariable("id")Integer id){
        System.out.println("--deleteById--");
        JsonData jsonData = new JsonData();
        int i = finanService.deletById(id);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 修改
     */
    @PutMapping("/updateById")
    public JsonData updateById(@RequestBody Finan finan){
        System.out.println("--updateById--");
        JsonData jsonData = new JsonData();
        int i = finanService.updateById(finan);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 添加
     */
    @PostMapping("/addFinan")
    public JsonData addFinan(@RequestBody Finan finan){
        System.out.println("--addFinan--");
        JsonData jsonData = new JsonData();
        int i = finanService.addFinan(finan);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 多条件查询
     */
    @GetMapping("/selectAllSB/{comm}")
    public Map<Object,Object> selectAllSB(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                @RequestParam(value = "limit",defaultValue = "5")Integer pageSize,@PathVariable("comm") String comm){
        System.out.println("---selectAllSB---");
        HashMap<Object, Object> map = new HashMap<>();
        JsonData jsonData = new JsonData();
        PageHelper.startPage(page,pageSize);
        PageInfo<Finan> pageInfo = finanService.selectAllSB(comm);
        map.put("code",0);
        map.put("msg","");
        map.put("count",(int)pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    /**
     * 单查询
     */
    @PostMapping("/selectById/{id}")
    public JsonData selectById(@PathVariable("id")Integer id){
        System.out.println("---selectById---");
        JsonData jsonData = new JsonData();
        Finan finan = finanService.selectById(id);
        if (finan==null) {
            System.out.println("失败");
            return jsonData.buildFailure();
        }else {
            System.out.println("成功");
            return jsonData.buildSuccess(finan);
        }
    }
}
